﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace YJKArch.FormatPainter
{
	public partial class FormatPainterForm : System.Windows.Forms.Form
	{
		public FormatPainterForm(List<KeyValuePair<string, string>> parameterPairList)
		{
			this.m_ParameterPairList = parameterPairList;
			this.InitializeComponent();
			this.dbgPropertiesList.CurrentCellDirtyStateChanged += this.dbgPropertiesList_CurrentCellDirtyStateChanged;
		}

		public List<bool> CheckedStateList()
		{
			this.Refresh();
			List<bool> list = new List<bool>();
			foreach (object obj in ((IEnumerable)this.dbgPropertiesList.Rows))
			{
				bool item = Convert.ToBoolean((((DataGridViewRow)obj).Cells[0] as DataGridViewCheckBoxCell).EditedFormattedValue);
				list.Add(item);
			}
			return list;
		}

		private void FormatPainterForm_Load(object sender, EventArgs e)
		{
			this.m_Initialized = false;
			this.dbgPropertiesList.Rows.Clear();
			foreach (KeyValuePair<string, string> keyValuePair in this.m_ParameterPairList)
			{
				DataGridViewRow dataGridViewRow = new DataGridViewRow();
				DataGridViewCheckBoxCell dataGridViewCheckBoxCell = new DataGridViewCheckBoxCell();
				dataGridViewCheckBoxCell.Value = true;
				dataGridViewRow.Cells.Add(dataGridViewCheckBoxCell);
				DataGridViewTextBoxCell dataGridViewTextBoxCell = new DataGridViewTextBoxCell();
				dataGridViewTextBoxCell.Value = keyValuePair.Value;
				dataGridViewRow.Cells.Add(dataGridViewTextBoxCell);
				this.dbgPropertiesList.Rows.Add(dataGridViewRow);
			}
			this.ReadData();
			this.m_Initialized = true;
		}

		private void dbgPropertiesList_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
		{
			if (this.dbgPropertiesList.Columns[e.ColumnIndex].CellTemplate.GetType() == typeof(DataGridViewCheckBoxCell) && e.RowIndex > -1)
			{
				e.Handled = true;
				string key = this.m_ParameterPairList[e.RowIndex].Key;
				System.Drawing.Rectangle cellBounds = e.CellBounds;
				System.Drawing.Rectangle r = new System.Drawing.Rectangle(cellBounds.X + 20, cellBounds.Y, cellBounds.Width - 20, cellBounds.Height);
				e.Paint(e.CellBounds, DataGridViewPaintParts.All);
				StringFormat stringFormat = new StringFormat();
				stringFormat.LineAlignment = StringAlignment.Center;
				e.Graphics.DrawString(key, this.dbgPropertiesList.Font, new SolidBrush(this.dbgPropertiesList.ForeColor), r, stringFormat);
			}
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void FormatPainterForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			FormatPainterForm.keybd_event((byte)bVk, 0, dwFlags, 0);
			FormatPainterForm.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void FormatPainterForm_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				FormatPainterForm.SendMessage(FormatPainterManager._hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				return;
			}
			base.Focus();
		}

		private void btnSelAll_Click(object sender, EventArgs e)
		{
			foreach (object obj in ((IEnumerable)this.dbgPropertiesList.Rows))
			{
				((DataGridViewRow)obj).Cells[0].Value = true;
			}
		}

		private void btnSelNone_Click(object sender, EventArgs e)
		{
			foreach (object obj in ((IEnumerable)this.dbgPropertiesList.Rows))
			{
				((DataGridViewRow)obj).Cells[0].Value = false;
			}
		}

		private void btnSelReverse_Click(object sender, EventArgs e)
		{
			foreach (object obj in ((IEnumerable)this.dbgPropertiesList.Rows))
			{
				DataGridViewRow dataGridViewRow = (DataGridViewRow)obj;
				bool flag = Convert.ToBoolean(dataGridViewRow.Cells[0].Value.ToString());
				dataGridViewRow.Cells[0].Value = !flag;
			}
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void FormatPainterForm_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.SaveData();
		}

		private void ReadData()
		{
			string path = Path.GetTempPath() + "FormatPainterForm.dat";
			if (!File.Exists(path))
			{
				base.StartPosition = FormStartPosition.CenterScreen;
				return;
			}
			base.StartPosition = FormStartPosition.Manual;
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			int x = Convert.ToInt32(streamReader.ReadLine());
			int y = Convert.ToInt32(streamReader.ReadLine());
			base.Location = new System.Drawing.Point(x, y);
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "FormatPainterForm.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(base.Location.X);
			streamWriter.WriteLine(base.Location.Y);
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private void dbgPropertiesList_CurrentCellDirtyStateChanged(object sender, EventArgs e)
		{
			if (this.dbgPropertiesList.IsCurrentCellDirty)
			{
				this.dbgPropertiesList.CommitEdit(DataGridViewDataErrorContexts.Commit);
			}
		}

		private List<KeyValuePair<string, string>> m_ParameterPairList = new List<KeyValuePair<string, string>>();

		private bool m_Initialized;
	}
}
